/*
 * Copyright (C) 2018 Pingan, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.dashboard.service;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author baojimin518
 * @version 1.0
 * @create 2018/8/6 10:09
 */
public class CollectionClearTopN {

    static Logger LOG = LoggerFactory.getLogger(CollectionClearUtil.class);

    private static final String domain_st = "http://solrv3.an2.ipo.com";
//    private static final String domain_st = "http://10.59.72.71:8001";
//    private static final String domain_st = "http://10.59.72.71:8001";
    private static final String action_st = "/solr/admin/collections?action=DELETE&name=";

    private static  final String path = "D:\\collection.txt";

    private static String pattern = "\\S+_\\d+_\\d+";

    private static int limitNum = 0;


    public static void main(String... args) {

        List<String> list = new ArrayList();

        try (
                FileInputStream fis = new FileInputStream(new File(path ));
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis, "utf-8"));
        ) {
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                boolean matches = line.matches(pattern);
                if (matches) {
                    list.add(line);
                }
            }
            //分组
            Map<String, ArrayList<String>> collect = list.stream().collect(Collectors.toMap(str -> getKey(str), Lists::newArrayList,
                    (o, n) -> {
                        o.addAll(n);
                        return o;
                    })
            );
            doGet(collect);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static String getKey(String index) {
        return index.substring(0, index.lastIndexOf("_"));
    }

    public static void doGet(Map<String, ArrayList<String>> collect) throws URISyntaxException, IOException {
        collect.values().stream().map(list -> list.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList())).forEach(list ->
                {
                    if(limitNum < 0 ) {
                        limitNum = 0;
                    }
                    if (list.size() > limitNum + 1) {
                        for (int i = 0; i < list.size() - 1 - limitNum; i++) {
                            String res = null;
                            try {
                                res = HttpUtil.doGet(domain_st + action_st + list.get(i));
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            LOG.info("URI :{},result :{}", domain_st + action_st + list.get(i), res);
                        }
                    }
                }
        );
    }


}



